<script setup lang="ts">
import KnowledgeItem from './knowledge-item.vue'
import type {
  KnowledgeList,
  KnowledgeParams,
  KnowledgeType
} from '@/types/consult'
import { getKnowledgePage } from '@/api/consult'
import { ref } from 'vue'

const props = defineProps<{
  type: KnowledgeType
}>()

const list = ref<KnowledgeList>([]) // 数据
const loading = ref(false) // 加载中状态
const finished = ref(false) // 是否加载完毕

const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})

const onLoad = async () => {
  try {
    // 加载更多
    const res = await getKnowledgePage(params.value)
    list.value.push(...res.data.rows)
    if (params.value.current >= res.data.pageTotal) {
      finished.value = true
    } else {
      params.value.current++
    }
    loading.value = false
  } catch {
    loading.value = false
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!--   <van-cell v-for="item in list" :key="item" :title="item" /> -->
      <KnowledgeItem
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></KnowledgeItem>
    </van-list>
  </div>
  >
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
